package com.yishusmt.wechatapi.wechatdev.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;

/**
 * Base64加密和解密
 */
public class Base64Util {

    /**
     * BASE64加密
     *
     * @param plaintext 明文
     * @return 密文
     */
    public static String encryptString(final String plaintext) {

        try {
            final String ciphertext = Base64.getEncoder().encodeToString(plaintext.getBytes("utf-8"));
            return ciphertext;
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * BASE64解密
     *
     * @return 明文
     */
    public static String decryptString(final String ciphertext) {

        try {
            final byte[] asBytes = Base64.getDecoder().decode(ciphertext);
            return new String(asBytes, "utf-8");
        } catch (final UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;

    }
}
